package qing.controller.cms;

import static org.hamcrest.CoreMatchers.instanceOf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Condition;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import qing.controller.CommonController;
import qing.dao.hibernate.OrderHelper;
import qing.entity.Node;
import qing.entity.Person;
import qing.entity.Tpl;
import qing.service.NodeService;
import qing.service.TplService;


@Controller
@RequestMapping("/cms/node")
public class NodeController extends CommonController<Node, String> {

	@Autowired
	private TplService itpldao;

	
	
	@Override
	public List<Node> showlistjson(Map<String, Object> map) {
		return digui();
	}
	
	@Override
	public String addini(Map<String, Object> map) {
		//digui();
		 bind(map);
		return super.addini(map);
	}

	@Override
	public String editini(@PathVariable("id") String id, Map<String, Object> map) {
		bind(map);
		return super.editini(id, map);
	}

	private void bind(Map<String, Object> map) {
		map.put("nodelist", digui());
		map.put("node", new Node());
		List<Tpl> tpls = itpldao.list("filetype", 2);		
		map.put("tpl1", tpls);
		map.put("tpl2", itpldao.getddllist(3));

	}

	private List<Node> digui() {
		OrderHelper order=OrderHelper.create("layer,taxis");
		List<Node> data = idao.list();
		List<Node> resultList = new ArrayList<>();
		//System.out.println("查询前集合大小:" + data.size());
		long start = System.currentTimeMillis();
		for (Node testEntity : data) {

			if (testEntity.getPid().trim().length() == 0) {// 父级菜单开始添加
				resultList.add(testEntity);
				if (ifChilds(data, testEntity.getId())) {// 存在子集

					List<Node> childs = new ArrayList<>();
					childs = getChildList(data, testEntity.getId(), childs);
					//System.out.println("----->childs:" + childs);
					resultList.addAll(childs);
				}
			}
		}
		//System.out.println("resultList 的 Size:" + resultList.size());
		// 打印显示
		/*for (Node testEntity : resultList) {
			String s = "";
			for (int i = 1; i < testEntity.getLayer(); i++) {
				s = s + "\t";
			}
			System.out.println(s + "____" + testEntity.getTitle());
		}*/
		return resultList;
	}

	// 获取父id下的子集合
	private static List<Node> getChildList(List<Node> list, String pId, List<Node> reList) {
		for (Node testEntity : list) {
			if (testEntity.getPid().equals(pId)) {// 查询下级菜单
				reList.add(testEntity);
				if (ifChilds(list, testEntity.getId())) {
					getChildList(list, testEntity.getId(), reList);
				}
			}
		}
		return reList;
	}

	// 判断是否存在子集
	private static boolean ifChilds(List<Node> list, String pId) {
		boolean flag = false;
		for (Node testEntity : list) {
		//	System.out.println("data --flag:(" + (testEntity.getPid().equals(pId)) + ")---------  testEntity<" + pId
			//		+ " >:" + testEntity.getPid());

			if (testEntity.getPid().equals(pId)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	private void add(Node model, HttpServletRequest request) {
		if (model.getPid().trim().length() > 0) {
			model.setLayer(idao.get(model.getPid()).getLayer() + 1);
		}
		System.out.println("path:" + request.getContextPath());
		model.setTplch(itpldao.get(model.getTplch().getId()));
		model.setTplcon(itpldao.get(model.getTplcon().getId()));
		Person person = (Person) request.getSession().getAttribute("user");
		model.setAdduser(person.getUsername());
	}

	@Override
	public String add(Node u, HttpServletRequest request, Map<String, Object> map) {

		add(u, request);
		return super.add(u, request, map);
	}

	@Override
	public String edit(@Valid Node u, BindingResult result, HttpServletRequest request, Map<String, Object> map) {
		add(u, request);
		return super.edit(u, result, request, map);
	}
	
	@RequestMapping(value = "islock/{id}")
	public String islock(@PathVariable("id") String id) {
		Node node=idao.get(id);
		node.setDeleted(node.isDeleted()?false:true);
		idao.update(node);
		return "redirect:../list";
	}
	
	@ResponseBody
	@RequestMapping(value = "taxis", method = RequestMethod.POST)
	public String taxis(@RequestBody  List<Map<String,Object>> list) {
		for (Map<String, Object> map : list) {
			 System.out.println(map.get("id")+" ** "+map.get("value")+"** "+entityClass.getSimpleName()+"\r\n");
			 String hql="update Node obj set obj.taxis=? where obj.id=?";
			 idao.updateByHql(hql, Integer.parseInt(map.get("value").toString()) ,map.get("id").toString());
		}
			  
		
		return "{\"status\":1,\"msg\":\"\"}";
	}

}
